﻿#include "optionsdialog.h"
#include "ui_optionsdialog.h"

OptionsDialog::OptionsDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OptionsDialog)
{
    ui->setupUi(this);

    ui->lineEditDefaultPath->setText(QDir::toNativeSeparators(QApplication::applicationDirPath()) + QString("\\Log"));


    // 确认按钮功能
    connect(ui->dialogButtonBox, &QDialogButtonBox::accepted, this, [=](){
        struct _options options;
        getOptions(&options);
        emit confirm(options);
    });

    //取消按钮功能
    connect(ui->dialogButtonBox, &QDialogButtonBox::rejected, this, &OptionsDialog::close, Qt::UniqueConnection);

    // 路径选择按钮
    connect(ui->toolButton, &QToolButton::clicked, this, [=](){
        QFileDialog dialog(this);
        dialog.setFileMode(QFileDialog::Directory);
        if(dialog.exec())
        {
            ui->lineEditDefaultPath->setText(QDir::toNativeSeparators(dialog.directory().path()));
        }


    });
}

OptionsDialog::~OptionsDialog()
{
    delete ui;
}

void OptionsDialog::getOptions(pOPTIONS pOptions)
{
    if(NULL == pOptions)
    {
        pOptions = new struct _options;
    }

    if(NULL == pOptions->pWindowBackgroundColor)
    {
        pOptions->pWindowBackgroundColor = new QString;
    }
    *pOptions->pWindowBackgroundColor = ui->lineEditWindowBackgroundColor->text();

    if(NULL == pOptions->pToolbarBackgroundColor)
    {
        pOptions->pToolbarBackgroundColor = new QString;
    }
    *pOptions->pToolbarBackgroundColor = ui->lineEditToolbarBackgroundColor->text();

    if(NULL == pOptions->pDefaultPath)
    {
        pOptions->pDefaultPath = new QString;
    }
    *pOptions->pDefaultPath = ui->lineEditDefaultPath->text();

    if(NULL == pOptions->pMultipleCyles)
    {
        pOptions->pMultipleCyles = new int;
    }
    *pOptions->pMultipleCyles = ui->lineEditMultipleCycles->text().toInt();

    if(NULL == pOptions->pFont)
    {
        delete pOptions->pFont;
    }
    pOptions->pFont = new QFont(ui->fontComboBox->currentFont());

    if(NULL == pOptions->pRegularSaveTime)
    {
        pOptions->pRegularSaveTime = new int;
    }
    *pOptions->pRegularSaveTime = ui->lineEditRegularSaveTime->text().toInt();

    if(NULL == pOptions->pFontSize)
    {
        pOptions->pFontSize = new int;
    }
    *pOptions->pFontSize = ui->lineEditFontSize->text().toInt();
    pOptions->pFont->setPointSize(*pOptions->pFontSize);

    if(NULL == pOptions->pTitleLabelBackgroundColor)
    {
        pOptions->pTitleLabelBackgroundColor = new QString;
    }
    *pOptions->pTitleLabelBackgroundColor = ui->lineEditTitleLabelBackgroundColor->text();

    if(NULL == pOptions->pValueLabelBackgroundColor)
    {
        pOptions->pValueLabelBackgroundColor = new QString;
    }
    *pOptions->pValueLabelBackgroundColor = ui->lineEditValueLabelBackgroundColor->text();


    if(NULL == pOptions->pLabelTextColor)
    {
        pOptions->pLabelTextColor = new QString;
    }
    *pOptions->pLabelTextColor = ui->lineEditLabelTextColor->text();

    if(NULL == pOptions->pImageAreaBackgroundColor)
    {
        pOptions->pImageAreaBackgroundColor = new QString;
    }
    *pOptions->pImageAreaBackgroundColor = ui->lineEditImageAreaBackgroundColor->text();

    if(NULL == pOptions->pLegendAreaBackgroundColor)
    {
        pOptions->pLegendAreaBackgroundColor = new QString;
    }
    *pOptions->pLegendAreaBackgroundColor = ui->lineEditLegendAreaBackgroundColor->text();

    if(NULL == pOptions->pLegendLabelHeight)
    {
        pOptions->pLegendLabelHeight = new int;
    }
    *pOptions->pLegendLabelHeight = ui->lineEditLegendLabelHeight->text().toInt();

    return;
}


void OptionsDialog::setOptions(struct _options options)
{
    ui->lineEditWindowBackgroundColor->setText(*options.pWindowBackgroundColor);
    ui->lineEditToolbarBackgroundColor->setText(*options.pToolbarBackgroundColor);
    ui->lineEditDefaultPath->setText(*options.pDefaultPath);
    ui->lineEditMultipleCycles->setText(QString::number(*options.pMultipleCyles));
    ui->lineEditRegularSaveTime->setText(QString::number(*options.pRegularSaveTime));
    ui->fontComboBox->setFont(*options.pFont);
    ui->lineEditFontSize->setText(QString::number(*options.pFontSize));
    ui->lineEditTitleLabelBackgroundColor->setText(*options.pTitleLabelBackgroundColor);
    ui->lineEditValueLabelBackgroundColor->setText(*options.pValueLabelBackgroundColor);
    ui->lineEditLabelTextColor->setText(*options.pLabelTextColor);
    ui->lineEditImageAreaBackgroundColor->setText(*options.pImageAreaBackgroundColor);
    return;
}




